package com.nuoniu.sibanyun.controller.system;


import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.common.result.ResultStatusCode;
import io.swagger.annotations.Api;
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName FinalExceptionHandlerController
 * @Description TODO
 * @Authpr baiqiang
 * @Date 2020/5/12 15:53
 **/
@RestController
@Log4j2
@Api(tags="错误接口")
public class FinalExceptionHandlerController implements ErrorController {
    @Override
    public String getErrorPath() {
        return "/error";
    }

    @GetMapping(value = "/error")
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public Object error(HttpServletRequest request, HttpServletResponse response) {

        log.error("response error,httpCode:" + response.getStatus());
        // 错误处理逻辑
        int status = response.getStatus();
        if (status == 404) {
            return HttpResult.error(ResultStatusCode.REQUEST_NOT_FOUND.getCode(), "小伙子你有点调皮哦！(*^▽^*),404了");
        } else if (status == 500) {
            return HttpResult.error(ResultStatusCode.SYSTEM_ERR.getCode(), "小伙子你麻烦大了！(*^▽^*)，500了");
        } else if (status >= 100 && status < 200) {
            return HttpResult.error(ResultStatusCode.HTTP_ERROR_100.getCode(), null);
        } else if (status >= 300 && status < 400) {
            return HttpResult.error(ResultStatusCode.HTTP_ERROR_300.getCode(), null);
        } else if (status >= 400 && status < 500) {
            return HttpResult.error(ResultStatusCode.HTTP_ERROR_400.getCode(), null);
        } else {
            return HttpResult.error(ResultStatusCode.SYSTEM_ERR.getCode(), "小伙子你麻烦大了！(*^▽^*)");
        }
    }

}

